#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#include <stack>
#include <bitset>
#define F first
#define S second
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
ll s,k;
ll cal(ll s,ll k)
{
	ll ans = s * k;
    ll x = max(min((5 * k - s) / 40,k / 4),0ll);
	ans = max(ans,-80*x*x+ (20*k-4*s)*x + s*k);
    x = min(x+1,k/4);
	ans = max(ans,-80*x*x+ (20*k-4*s)*x + s*k);
    return ans;
}
ll solve()
{
    cin >> s >> k;
    if(s % 10 == 0) return s * k;
    if(s % 10 == 5) return max(s*k,(s+5)*(k-1));
    ll ans = s * k;
    if(s % 2) s += s % 10,k --;
    for(int i = 1;i <= 4;i ++){
        if(k <= 0) break;
        ans = max(ans,cal(s,k)),s += s % 10,k --;
    }
    return ans;
}
int main()
{
	IOS;
	int T=1;
	cin>>T;

	while(T--)
	{
		cout<<solve()<<'\n';
	}

	return 0;
}

